package org.ht.viewer;

import java.awt.Frame;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.io.File;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JToolBar;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;

import org.azj.logHelper.LOG;
import org.ht.combotable.ComboTableFactory;
import org.ht.global.Global;
import org.swixml.SwingEngine;

public class MainFrame {
	JMenu menu;
	JMenuItem newData;
	JToolBar toolbar;
	StatusBar statusBar;
	JScrollPane scrollpane;
	SwingEngine swix = new SwingEngine(this);

	JFrame frame;

	/*
	 * 主窗口
	 */
	public MainFrame() throws Exception {
		// 窗口界面的XML的路径文件
		LOG.WriteLog(System.getProperty("user.dir"));
		String configs = System.getProperty("user.dir")
				+ "\\xml\\MainFrame.xml";
		// 根据XML自动生成frame并注入
		frame = (JFrame) swix.render(new File(configs));

		frame.setExtendedState(Frame.MAXIMIZED_BOTH);
		frame.getContentPane().add(toolbar, "North");
		statusBar = new StatusBar();
		frame.getContentPane().add(statusBar, "South");

		addTable();
		frame.setVisible(true);
		Global.mainpanel = frame.getContentPane();
		Global.statusBar = statusBar;
	}

	/**
	 * 添加表格控件
	 */
	private void addTable() {
		// 用工厂方法创建,此处用到默认查询
		Global.table = ComboTableFactory.instance.getDefaultQuery();
		frame.add(new JScrollPane(Global.table));
		Global.table.getModel().addTableModelListener(new TableModelListener() {

			@Override
			public void tableChanged(TableModelEvent e) {
				Object source = e.getSource();
				if (source instanceof TableModel) {
					TableModel model = (TableModel) source;
					statusBar.setTotallineText(String.valueOf(model
							.getRowCount()));
				}
			}
		});

		Global.table.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent e) {
				// 获取鼠标点击的行的位置（及行数）
				Point mousepoint;
				mousepoint = e.getPoint();
				int nowline = Global.table.rowAtPoint(mousepoint) + 1;
				statusBar.setNowline(String.valueOf(nowline));
			}
		});
	}

	/*
	 * 新建病例
	 */
	public Action newAction = new AbstractAction() {
		private static final long serialVersionUID = 1L;

		public void actionPerformed(ActionEvent e) {
			LOG.WriteLog("新建病例");
			CaseDialog caseDialog = new CaseDialog(null, CaseDialog.TYPE_NEW);
			caseDialog.setVisible(true);
		}
	};

	/*
	 * 查询病例
	 */
	public Action queryAction = new AbstractAction() {
		private static final long serialVersionUID = 1L;

		public void actionPerformed(ActionEvent e) {
			LOG.WriteLog("查询病例");
			QueryDialog queryDialog = new QueryDialog();
			queryDialog.setVisible(true);
		}
	};

	public Action aboutAction = new AbstractAction() {
		private static final long serialVersionUID = 1L;

		public void actionPerformed(ActionEvent e) {
			JOptionPane.showMessageDialog(swix.getRootComponent(), "作者 Aicro,Hunter QQ:110350328");
		}
	};
}
